//
//  ChivoxWsEngine.m
//  ChivoxEngine
//
//  Created by xianchen.peng on 2018/8/29.
//  Copyright © 2018年 Chivox. All rights reserved.
//

#if 0

#import "ChivoxWsService.h"
#import <CommonCrypto/CommonDigest.h>
#import "SocketRocket.h"
#define dispatch_main_async_safe(block)\
if ([NSThread isMainThread]) {\
block();\
} else {\
dispatch_async(dispatch_get_main_queue(), block);\
}
@implementation ChivoxWsService
- (void)connect:(NSDictionary *)cfg{
    NSString *url = [[NSString alloc]init];
    self.socket = [[SRWebSocket alloc]initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]] ];
    self.socket.delegate = self;
    [self.socket open];
}

-(void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message{
    if (webSocket == self.socket) {
        NSLog(@"message:%@",message);
        [self.engineDagete onResult:@"1" text:message];
    }
}
-(void)webSocket:(SRWebSocket *)webSocket didReceiveMessageWithString:(NSString *)string{
    NSLog(@"%@",string);
}
-(void)webSocket:(SRWebSocket *)webSocket didReceiveMessageWithData:(NSData *)data{
    NSLog(@"%@",data);
}
- (void)webSocketDidOpen:(SRWebSocket *)socket{
    NSLog(@"连接成功");
}
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
    NSLog(@"连接失败：%@",error);
    
}
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
    NSLog(@"连接断开");
}
#define WeakSelf(ws) __weak __typeof(&*self)weakSelf = self

- (void)feed:(NSData *)data {
    WeakSelf(ws);
    dispatch_queue_t queue =  dispatch_queue_create("zy", NULL);
    
    dispatch_async(queue, ^{
        if (weakSelf.socket != nil) {
            // 只有 SR_OPEN 开启状态才能调 send 方法啊，不然要崩
            if (weakSelf.socket.readyState == SR_OPEN) {
                
                    [weakSelf.socket sendData:data error:nil];
                
            } else if (weakSelf.socket.readyState == SR_CONNECTING) {
                
            } else if (weakSelf.socket.readyState == SR_CLOSING || weakSelf.socket.readyState == SR_CLOSED) {
            }
        } else {
            
        }
    });
}
- (void)initCloud:(NSString *)cfg{
    WeakSelf(ws);
    dispatch_queue_t queue =  dispatch_queue_create("zy", NULL);
    
    dispatch_async(queue, ^{
        if (weakSelf.socket != nil) {
            // 只有 SR_OPEN 开启状态才能调 send 方法啊，不然要崩
            if (weakSelf.socket.readyState == SR_OPEN) {
                
                [weakSelf.socket sendString:cfg error:nil];
                
            } else if (weakSelf.socket.readyState == SR_CONNECTING) {
                
            } else if (weakSelf.socket.readyState == SR_CLOSING || weakSelf.socket.readyState == SR_CLOSED) {
            }
        } else {
            
        }
    });
}

- (void)start:(NSString *)param{
    WeakSelf(ws);
    dispatch_queue_t queue =  dispatch_queue_create("zy", NULL);
    
    dispatch_async(queue, ^{
        if (weakSelf.socket != nil) {
            // 只有 SR_OPEN 开启状态才能调 send 方法啊，不然要崩
            if (weakSelf.socket.readyState == SR_OPEN) {
                
                [weakSelf.socket sendString:param error:nil];
                
            } else if (weakSelf.socket.readyState == SR_CONNECTING) {
                
            } else if (weakSelf.socket.readyState == SR_CLOSING || weakSelf.socket.readyState == SR_CLOSED) {
            }
        } else {
            
        }
    });
}
-(void)stop{
 NSMutableData *data = [NSMutableData dataWithLength:0];
    WeakSelf(ws);
    dispatch_queue_t queue =  dispatch_queue_create("zy", NULL);
    
    dispatch_async(queue, ^{
        if (weakSelf.socket != nil) {
            // 只有 SR_OPEN 开启状态才能调 send 方法啊，不然要崩
            if (weakSelf.socket.readyState == SR_OPEN) {
                
                [weakSelf.socket sendData:data error:nil];
                
            } else if (weakSelf.socket.readyState == SR_CONNECTING) {
                
            } else if (weakSelf.socket.readyState == SR_CLOSING || weakSelf.socket.readyState == SR_CLOSED) {
            }
        } else {
            
        }
    });
}
- (void)disconnect:(NSDictionary *)cfg{
     [self.socket close];
    
}


@end

#endif
